#include "MainWindow.h"
#include "Platform.h"
#include "AutoImportWindow.h"
#include <QDesktopServices>
#include <QDebug>
#include "Version.h"

MainWindow::MainWindow(QWidget* parent) : QWidget(parent)
{
    versionLabel = new QLabel(this);
    versionLabel->setText(VERSION);
    versionLabel->setStyleSheet("QLabel {color: white;}");
    versionLabel->raise();

    backgroundImage = new QLabel(this);
    backgroundImage->setPixmap(QPixmap::fromImage(QImage(":/resource/title.png")));
    backgroundImage->setMargin(0);
    backgroundImage->lower();

    contentDirButton = new QPushButton(this);
    contentDirButton->setText(tr("content_dir"));
    connect(contentDirButton, &QPushButton::clicked, [](bool)
    {
        QDesktopServices::openUrl(QUrl::fromLocalFile(Platform::GetOraContentDir()));
    });

    mapDirButton = new QPushButton(this);
    mapDirButton->setText(tr("map_dir"));
    connect(mapDirButton, &QPushButton::clicked, [](bool)
    {
        QDesktopServices::openUrl(QUrl::fromLocalFile(Platform::GetOraMapDir()));
    });

    logDirButton = new QPushButton(this);
    logDirButton->setText(tr("log_dir"));
    connect(logDirButton, &QPushButton::clicked, [](bool)
    {
        QDesktopServices::openUrl(QUrl::fromLocalFile(Platform::GetOraLogDir()));
    });

    autoImportButton = new QPushButton(this);
    autoImportButton->setText(tr("auto_import"));
    connect(autoImportButton, &QPushButton::clicked, this, [this](bool)
    {
        auto sub_window = new AutoImportWindow(this);
        sub_window->setAttribute(Qt::WA_DeleteOnClose);
        sub_window->setWindowModality(Qt::WindowModal);
        sub_window->show();
    });

    buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();
    buttonLayout->addWidget(contentDirButton);
    buttonLayout->addStretch();
    buttonLayout->addWidget(mapDirButton);
    buttonLayout->addStretch();
    buttonLayout->addWidget(logDirButton);
    buttonLayout->addStretch();
    buttonLayout->addWidget(autoImportButton);
    buttonLayout->addStretch();

    mainLayout = new QVBoxLayout();
    mainLayout->addWidget(backgroundImage);
    mainLayout->addStretch();
    mainLayout->addLayout(buttonLayout);
    mainLayout->addStretch();
    mainLayout->setContentsMargins(0, 0, 0, 0);

    setContentsMargins(0, 0, 0, 0);
    setLayout(mainLayout);
    setWindowFlags(windowFlags() &~ Qt::WindowMaximizeButtonHint);
    setFixedSize(666, 200);
    setWindowTitle(tr("title"));
    setWindowIcon(QIcon("://resource/icon.png"));
}

MainWindow::~MainWindow()
{
    delete mainLayout;
    delete versionLabel;
    delete backgroundImage;
    delete contentDirButton;
    delete mapDirButton;
    delete logDirButton;
}
